<template>
  <div class="pagination-container">
      <el-pagination 
        :background="background" 
        :current-page="query.current" 
        :total="query.total"
        :page-size="query.limit" 
        :page-sizes="pageSizes" 
        :layout="layout" 
        @size-change="handleSizeChange" 
        @current-change="handleCurrentChange" 
        />
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scrollTo'

export default {
  name: 'Pagination',
  props: {
    query: {
      type: Object,
      default: () => {}
    },
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 30, 50]
      }
    },
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    handleSizeChange(val) {
      this.query.limit = val
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
      this.initList()
    },
    handleCurrentChange(val) {
      this.query.current = val;
      this.query.start = (val - 1) * this.query.limit
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
      this.initList()
    },
    initList() {
      this.$emit('initList');
    }
  }
}
</script>
